<template>
  <div class="article">
    
<div id="app">
 
   <el-button type="primary" icon="el-icon-timer" style="width:100%">{{date}}</el-button>

</div>

<br>


    <!-- 按钮 -->
    <div class="btns">
      <el-button size="small" type="primary" @click="toAdd" icon="el-icon-edit">添加</el-button>
    </div>
    <!-- 表格 -->
    <el-table :data="list" style="width: 100%" size="small">
      <el-table-column type="index" label="序号"> </el-table-column>
      <el-table-column prop="title" label="标题"> </el-table-column>
      <el-table-column prop="content" label="正文" width="300"> </el-table-column>
      <el-table-column prop="publishDate" label="推送时间"> </el-table-column>
      <el-table-column prop="readTimes" label="阅读次数"> </el-table-column>
      <el-table-column prop="thumbUpTimes" label="属性" > </el-table-column>
      <el-table-column prop="thumbDownTimes" label="点击量"> </el-table-column>
      <el-table-column prop="status" label="作者" > </el-table-column>
      <el-table-column prop="categoryId" label="关键字"> </el-table-column>
      <el-table-column prop="userId" label="来源"> </el-table-column>
      <el-table-column label="操作" width="150" align="center"> 
        <template v-slot="scope">
          <el-button size="small" type="danger" @click="del(scope.row)">删除</el-button>
          <el-button size="small" type="primary" @click="edit(scope.row)">修改</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 模态框 -->
    <el-dialog
      class="customer_modal"
      title="添加article"
      :visible.sync="dialogVisible"
      width="60%">
      {{form}}
      <el-form :model="form" label-width="100px">
        <el-form-item label="标题" >
          <el-input v-model="form.title"></el-input>
        </el-form-item>
        <el-form-item label="正文" >
          <el-input v-model="form.content"></el-input>
        </el-form-item>
        <el-form-item label="推送时间" >
          <el-input v-model="form.publishDate"></el-input>
        </el-form-item>
        <el-form-item label="阅读次数" >
          <el-input v-model="form.readTimes"></el-input>
        </el-form-item>
        <el-form-item label="属性" >
          <el-input v-model="form.thumbUpTimes"></el-input>
        </el-form-item>
        <el-form-item label="点击量" >
          <el-input v-model="form.thumbDownTimes"></el-input>
        </el-form-item>
        <el-form-item label="作者" >
          <el-input v-model="form.status"></el-input>
        </el-form-item>
        <el-form-item label="关键字" >
          <el-input v-model="form.categoryId"></el-input>
        </el-form-item>
        <el-form-item label="来源" >
          <el-input v-model="form.userId"></el-input>
        </el-form-item>

      </el-form>
      
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false" size="small">取 消</el-button>
        <el-button type="primary" @click="save" size="small">确 定</el-button>
      </span>
    </el-dialog>

  </div>
</template>

<script>
// 导入request , 浏览器提供给我们的用于与后台进行交互的工具 api
import {get,post} from '@/utils/request'
export default {
  data (){
    return {
       date: new Date(),
      form:{},
      list:[],
      dialogVisible:false
    }
  },
  mounted() {
    let _this = this; // 声明一个变量指向Vue实例this，保证作用域一致
    this.timer = setInterval(() => {
      _this.date = new Date(); // 修改数据date
    }, 1000)
  },
  beforeDestroy() {
    if (this.timer) {
      clearInterval(this.timer); // 在Vue实例销毁前，清除我们的定时器
    }
  },
  created(){
    this.loadData();
  },
  methods:{
    // 保存
    save(){
      let url = "/article/saveOrUpdate"
      post(url,this.form).then( (response)=>{
        // 1. 模态框关闭
        this.dialogVisible = false;
        // 2. 提示
        this.$message({type:"success",message:response.message})
        // 3. 刷新
        this.loadData();
      } )
    },
    // 删除
    del(row){
       let url = "/article/deleteById"
      post(url,{id:row.id}).then( (response)=>{
        // 1. 提示
        this.$message({type:"success",message:response.message})
        // 2. 刷新 
        this.loadData();
      } )
      // this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
      //     confirmButtonText: '确定',
      //     cancelButtonText: '取消',
      //     type: 'warning'
      //   }).then(() => {
      //     let url = "/article/deleteById"
      //     get(url,{id:row.id}).then( (response)=>{
      //       // 1. 提示
      //       this.$message({type:"success",message:response.message})
      //       // 2. 刷新 
      //       this.loadData();
      //     } )
      //   })
    },
    // 编辑
    edit(row){
      this.form = row;
      this.dialogVisible = true;
    },
    // 加载数据
    loadData(){
      let url = "/article/findAll";
      get(url).then( (response)=>{
        // response就是后端返回给我们的Message{status,message,data,timestamp}
        this.list = response.data;
      } );
    },
    toAdd(){
      this.dialogVisible = true;
    }
  }  
 
}
</script>

